App.require(['strTool'], function (strTool) {
    var app = App.getCurrent();

    app.definePage("strdatetimePickerSecond", {
        getDomObj: function () {
            this.attachDom(".str-text-field", "textField")
                .attachDom("input", "input")
                .attachEvent("input", "focus", this.focusHandler, false)
                .attachEvent("input", "keydown", this.keyDownHandler, false)
                .attachEvent("input", "input", this.inputHandler, false);

            this.domList.endTimeInput = this.getComponentById("entertime").nodes[0].querySelector("input");
            this.attachEvent("endTimeInput", "keydown", this.timeKeydownHandler, false)
                .attachEvent("endTimeInput", "focus", this.timeFocusHandler, false)
                .attachEvent("endTimeInput", "input", this.timeInputHandler, false);
        },
        focusHandler: function () {
            this.parent.staticPage.activeSelect("date");
        },
        timeFocusHandler: function () {
            this.parent.staticPage.activeSelect("time");
        },
        selectDate: function () {
            this.getComponentById("enterdate").focus();
        },
        selectTime: function () {
            this.getComponentById("entertime").focus();
        },
        timeKeydownHandler: function (ev) {
            if (!/\d/.test(ev.key) && ev.key != "Backspace" && ev.key != "ArrowLeft" && ev.key != "ArrowRight") ev.preventDefault();
        },
        timeInputHandler: function (ev) {
            var target = ev.target, value = target.value;
            value = ("" + value).replace(/:/g, "");
            if (value.length == 1 && parseInt(value) > 2) {
                value = "0" + value + ":";
            }
            else if (value.length > 2) {
                var minute = value.slice(2), hour = value.slice(0, 2);
                hour = parseInt(hour) > 23 ? "23" : hour;
                if (minute.length == 1 && parseInt(minute) > 5) minute = "0" + minute;
                else minute = parseInt(minute) > 59 ? "59" : minute;
                value = hour + ":" + minute;
            }
            target.value = value;

            if (value.length == 5) {
                this.parent._computedTime(parseInt(value.slice(0, 2)), parseInt(value.slice(3)));
            }
        },
        keyDownHandler: function (ev) {
            if (!/\d/.test(ev.key) && ev.key != "Backspace" && ev.key != "ArrowLeft" && ev.key != "ArrowRight") ev.preventDefault();
            var value = ev.target.value;
            if (value.length == 9 && parseInt(value.slice(8)) == 3) {
                if (parseInt(ev.key) > 1) ev.preventDefault();
            }
        },
        inputHandler: function (ev) {
            var target = ev.target, value = target.value;
            value = ("" + value).replace(/-/g, "");
            if (value.length > 4) value = value.slice(0, 4) + "-" + value.slice(4);
            if (value.length > 7) value = value.slice(0, 7) + "-" + value.slice(7);
            if (value.length == 6) {
                var num = parseInt(value.slice(5));
                if (num > 1) value = value.slice(0, 5) + "0" + num + "-";
            }
            if (value.length == 7) {
                var num = parseInt(value.slice(5)), tNum = parseInt(num / 10), zNum = num % 10;
                if (num > 12) value = value.slice(0, 5) + "0" + tNum + "-" + zNum;
            }
            if (value.length == 9) {
                var num = parseInt(value.slice(8));
                if (num > 3) value = value.slice(0, 8) + "0" + num;
            }
            target.value = value;

            if (value.length == 10) {
                this.parent._computedDate(parseInt(value.slice(0, 4)), parseInt(value.slice(0, 4)), parseInt(value.slice(8, 10)));
            }
        },
        render: function (next) {
            var parentData = this.parent.data;
            this.data.yDate = parentData.yDate;
            this.data.yTime = parentData.yTime;
            this.data.minTime = parentData.minTime;
            this.data.maxTime = parentData.maxTime;
            this.data.minDate = parentData.minDate;
            this.data.maxDate = parentData.maxDate;
            this.fetch("./index.html", function (html) {
                next(html);
            })
        },
        init: function () {
            this.parent.staticPage.activeSelect("date");
            var input = this.domList.input, textField = this.domList.textField;
            textField.classList.add("str-text-edit");
            textField.classList.add("str-text-color");
            input.focus();
            input.value = this.data.yDate.toFormatString();

            var enterTime = this.getComponentById("entertime");
            var yTime = this.data.yTime;
            enterTime.value = strTool.wrap(yTime.hour, "0", 2) + ":" + strTool.wrap(yTime.minute, "0", 2);
        },
    })
});